<?php

/**
 * Product form.
 *
 * @package    Web20
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProductForm extends BaseProductForm
{

  public function configure()
  {

	$this->setWidgets(array(
       	'id'	  	 	=> new sfWidgetFormInputHidden(),
      	'site_id'		=> new sfWidgetFormInputHidden(),
      	'name'			=> new sfWidgetFormInput(array('label' => "Nombre"), array('size' => 83)),
      	'price'			=> new sfWidgetFormInput(array('label' => "Precio")),
		'description'	=> new sfWidgetFormTextareaTinyMCE(array(
							'label' => "Descripción",
							'width' => 350,
							'height' => 250,
							'theme' => "simple",
							'config' => 'theme_advanced_disable: "anchor,image,cleanup,help"',
							)),
		'new_tags'		=> new sfWidgetFormInput(
		       array('label'   =>  'Agregar tags', 'default'   =>  'Agregar tags separados por comas'),
		       array(
		             'onclick' => "if (this.value=='Agregar tags separados por comas') {
		                                this.value = ''; this.style.color='black'; }",
		             'size'   =>  '83',
			     'id' => 'new_tags',
		             // don't let the browser autocomplete.  We'll add typeahead, below
			     'autocomplete' => "off",
			     'style' => 'color:#aaa'
			     )),
		'remove_tags'	=> new sfWidgetFormInputHidden(),

	));

	$this->embedForm('files', new ProductFileUploadsForm($this->getObject()));

	$this->setValidator('name', new sfValidatorString(array('required' => true, 'max_length' => 50)));
	$this->setValidator('price', new sfValidatorNumber(array('required' => true, 'min' => 0)));
	$this->setValidator('new_tags', new sfValidatorString(array('required' => false)));
	$this->setValidator('remove_tags', new sfValidatorString(array('required' => false)));
	$this->setValidator('description', new sfValidatorString(array('required' => false)));

	$this->widgetSchema->setNameFormat('product[%s]');
	$this->validatorSchema->setOption('allow_extra_fields', true);
  }


  /*
  public function updateObjectEmbeddedForms($values, $forms = null)
  {
    if (is_array($forms))
    {
      foreach ($forms as $key => $form)
      {
        if ($form instanceof ProductPictureForm)
        {
          $formValues = isset($values[$key]) ? $values[$key] : array();

          if (ProductPictureForm::formValuesAreBlank($formValues)) unset($forms[$key]);
        }
      }
    }

    return parent::updateObjectEmbeddedForms($values, $forms);
  }
  */

  public function saveEmbeddedForms($con = null, $forms = null)
  {
  	if (is_null($con))
    {
      $con = $this->getConnection();
    }

    if (is_null($forms))
    {
      $forms = $this->embeddedForms;
    }

    if (is_array($forms))
    {
      foreach ($forms as $key => $form)
      {
        if ($form instanceof ProductPictureForm)
        {
		  if ( $form->getValue('file_delete') )
		  {
			  $filename = $form->getObject()->getFileName();
			  @unlink($filename);
			  $form->getObject()->delete();

			  $thumbnailpath = $form->getObject()->getThumbnailFileName();
			  @unlink($thumbnailpath);
		  }elseif ($form->getObject()->isModified() || $this->taintedFiles['files'][$key]['file']['error'] == 0)
          {
            $form->getObject()->Product = $this->object;
          }
          if ($this->taintedFiles['files'][$key]['file']['error'] != 0)
          {
            unset($forms[$key]);
          }
        }
      }
    }

    return parent::saveEmbeddedForms($con, $forms);

  }

}
